package com.sys.management.mapper.service;

import com.sys.management.mapper.EvaluationMapper;
import com.sys.management.pojo.employeeinformation;
import com.sys.management.pojo.evaluationmanagement;
import com.sys.management.pojo.evaluationvo;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;

/*@SpringBootTest(classes = ManagementApplication.class)
@RunWith(SpringRunner.class)*/
@Service
@Transactional
public class evaluationService{

    @Resource
    private EvaluationMapper evaluationMapper;

    @Resource
    private informationService inser;

    //增加员工评价
    public void save(evaluationmanagement emp){
        evaluationMapper.insert(emp);
    }

    //查询所有员工
    public List<evaluationmanagement> searchAll(){
        List<evaluationmanagement> evaluationmanagements = evaluationMapper.selectList(null);
        return evaluationmanagements;
    }

    public List<evaluationvo> mutiSearch(){
        return evaluationMapper.evaluationVo();
    }


    public List<evaluationmanagement> searchById(String id){
        HashMap<String,Object> map=new HashMap<>();
        map.put("eid",id);

        List<evaluationmanagement> evaluationmanagements = evaluationMapper.selectByMap(map);

        return evaluationmanagements;
    }

    //判断是否有该员工，有则添加评论
    public boolean isExist(evaluationmanagement eva){
        List<employeeinformation> infos = inser.selectall();
        for(employeeinformation info:infos){
            if(info.getEid().equals(eva.getEid())) {//只有基本信息中有的员工才能添加
                evaluationMapper.insert(eva);
                return true;
            }
        }
        return false;
    }

    /**
     * 获取下载模版
     */
    public void exportExel(String fileName, List<evaluationvo>evaluationList) throws Exception{
        //没有元素，则不进行下载
        if (evaluationList.size()<=0)
            return;
        //新建一个工作簿
        Workbook workbook = new HSSFWorkbook();
        //创建sheet;
        Sheet sheet = workbook.createSheet();
        Row rowT = sheet.createRow(0);
        Cell cellT1 = rowT.createCell(0);
        cellT1.setCellValue("员工编号");
        Cell cellT7 = rowT.createCell(1);
        cellT7.setCellValue("姓名");
        Cell cellT8 = rowT.createCell(2);
        cellT8.setCellValue("部门");
        Cell cellT2 = rowT.createCell(3);
        cellT2.setCellValue("是否违纪");
        Cell cellT3 = rowT.createCell(4);
        cellT3.setCellValue("违纪说明");
        Cell cellT4 = rowT.createCell(5);
        cellT4.setCellValue("有无奖励");
        Cell cellT5 = rowT.createCell(6);
        cellT5.setCellValue("奖励说明");
        Cell cellT6 = rowT.createCell(7);
        cellT6.setCellValue("主管评价");

        for (int i = 0;i<evaluationList.size();i++){
            Row row = sheet.createRow(i+1);
            Cell cell1 = row.createCell(0);
            cell1.setCellValue(evaluationList.get(i).getEid());
            Cell cell7 = row.createCell(1);
            cell7.setCellValue(evaluationList.get(i).getEname());
            Cell cell8 = row.createCell(2);
            cell8.setCellValue(evaluationList.get(i).getEdepartment());
            Cell cell2 = row.createCell(3);
            cell2.setCellValue(evaluationList.get(i).getIsbreak());
            Cell cell3 = row.createCell(4);
            cell3.setCellValue(evaluationList.get(i).getBreakexplain());
            Cell cell4 = row.createCell(5);
            cell4.setCellValue(evaluationList.get(i).getIsreward());
            Cell cell6 = row.createCell(6);
            cell6.setCellValue(evaluationList.get(i).getRewardexplain());
            Cell cell5 = row.createCell(7);
            cell5.setCellValue(evaluationList.get(i).getEvacontent());


        }
        fileName = fileName +".xls";
        FileOutputStream fout = new FileOutputStream(fileName);
        workbook.write(fout);
        fout.close();
        workbook.close();
    }
}


